﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;
using System.Text;
using System.Collections;

namespace VSM
{
    //WIN-QG271FIL5FQ
    public partial class ReminderList : System.Web.UI.Page
    {
        
        private static GeneralFunctions GF = null;
        private static string dbType;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[Utils.AppConstants.ID_User] == null
                          || Session[Utils.AppConstants.Password] == null
                          || Session[Utils.AppConstants.ID_Dealer] == null)
            {
                Response.Redirect("../../Login.aspx");
            }

            if (!IsPostBack)
            {
                dbType = Session[Utils.AppConstants.ID_Dealer].ToString() == "VSM" ? Utils.AppConstants.VSM : Utils.AppConstants.DEALER;
                
                LoadDefaultData();
                
                if (Request.QueryString["id"] != null && Request.QueryString["id"].Length > 0)
                {
                  
                }
              
            }
        }

        private void LoadDefaultData()
        {
            ddlDay.Items.Add("75");
            ddlDay.Items.Add("90");
            ddlDay.Items.Add("105");
            ddlDay.Items.Add("120");

            Services sv = new Services();
            string sql = "select '-1' as Code, 'All' as Name, '' as U_Position, '' as U_DealerCode ";
            sql += " union all ";
            sql += " select Code, Name, U_Position, U_DealerCode from [@TECHNICIAN] ";
            sql += " where U_DealerCode = '" + Session[Utils.AppConstants.ID_Dealer] + "'";

            DataSet dsAdviser = sv.ExecuteQuery("VSM", sql);
            ddlAdviser.DataSource = dsAdviser.Tables[0];
            ddlAdviser.DataTextField = "Name";
            ddlAdviser.DataValueField = "Code";
            ddlAdviser.DataBind();
            DataTable tbStatus = new DataTable();
            tbStatus.Columns.Add("StatusCode");
            tbStatus.Columns.Add("StatusName");
            tbStatus.Rows.Add(-1, "All");
            tbStatus.Rows.Add(0, "Pending");
            tbStatus.Rows.Add(1, "Reminded");

            ddlStatus.DataSource = tbStatus;
            ddlStatus.DataTextField = "StatusName";
            ddlStatus.DataValueField = "StatusCode";
            ddlStatus.DataBind();

            ddlStatus.Items.FindByValue("0").Selected = true;
        }
       
        protected void btn_Filter_Click(object sender, EventArgs e)
        {
            loadDataFilter();
        }

        private void loadDataFilter()
        {
            string numofDate = txt_NumOfDays.Text.Trim();

            string selectedDate = "0";
            if (numofDate == "")
                selectedDate = ddlDay.SelectedItem.Text;
            else
                selectedDate = numofDate;

            int oneMoreWeek = Int32.Parse(selectedDate) + 7;

            string sql = " select ROW_NUMBER() OVER (ORDER BY JobID) AS RowNum, ";
            sql += " ServiceDate, gs.GeneralID, gs.JobID, Receptionist, jk.Name JobKindName, AdviserCode, tec.Name AdviserName, TechnicianCode, ";
            sql += " VIN, VehicleName, RegNo, CustomerName, Address, Tel, Mobile, ContactPerson, ContactPhone, isnull(IsReminded, 0) IsReminded ";
            sql += " from U_GeneralService gs ";
            sql += " join VSM_ORG..[@JOBKINDSVC] jk on jk.Code = gs.JobKind ";
            sql += "  join VSM_ORG..[@TECHNICIAN] tec on tec.Code = gs.AdviserCode ";
            sql += " where isnull(gs.WSCType, 'A') = 'A' ";
            sql += " AND DATEDIFF(day, ServiceDate, GETDATE()) between " + selectedDate + " and " + oneMoreWeek;
            sql += " AND gs.DealerCode = '" + Session[Utils.AppConstants.ID_Dealer] + "' ";
            if (ddlStatus.SelectedValue != "-1")
                sql += " and isnull(IsReminded, 0) = " + ddlStatus.SelectedValue;

            if (ddlAdviser.SelectedValue != "-1")
                sql += " and gs.AdviserCode = " + ddlAdviser.SelectedValue;

            Services sv = new Services();
            DataTable dt = sv.ExecuteQuery(dbType, sql).Tables[0];

            grvPO.DataSource = dt;
            grvPO.DataBind();
        }

        protected void btn_Done_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.grvPO.Rows.Count; i++)
            {
                CheckBox checkBox = (CheckBox)this.grvPO.Rows[i].FindControl("chkDone");

                if (checkBox != null)
                {
                    if (checkBox.Checked)
                    {
                        Label gID = (Label)this.grvPO.Rows[i].FindControl("lbGeneralID");

                        Services sv = new Services();
                        sv.ExecuteQuery("DEALER", "UPDATE U_GeneralService SET IsReminded = 1 WHERE GeneralID = " + gID.Text);
                    }
                }
            }
            ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "CallJS", "alert('Update Complete,');", true);
            loadDataFilter();
        }
    }
}